package com.dz.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dz.reggie.dto.DishDto;
import com.dz.reggie.dto.SetmealDto;
import com.dz.reggie.entity.Category;
import com.dz.reggie.entity.Setmeal;
import com.dz.reggie.entity.SetmealDish;
import com.dz.reggie.mapper.SetmealMapper;
import com.dz.reggie.service.DishService;
import com.dz.reggie.service.SetmealDishService;
import com.dz.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Transactional
    @Override
    public void saveWithDishes(SetmealDto setmealDto) {
        log.info("saveWithDishes方法中{}", setmealDto);
        // 保存套餐基本信息，操作setmeal，执行insert操作 -- 套餐id的自动生成是在主键配置策略中已经配置完成
        this.save(setmealDto);

        // 遍历套餐菜品，为菜品绑定套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        // 保存套餐和菜品的关联关系操纵setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，也需要操作两张表：Setmeal和Setmeal_Dish
     * 即先删除套餐表中信息，再遍历Setmeal_Dish表按套餐id删除关联的菜品
     */
    @Transactional
    @Override
    public void deleteWithDishes(ArrayList<Long> idList) {

        // 1 菜品口味表中根据dish_id批量删除
        HashMap<String, Object> map = new HashMap<>();
        // todo - 改善 如何批量删除，以非主键字段为条件的表中数据
        for (Long id : idList) {
            map.put("setmeal_id", id);
            setmealDishService.removeByMap(map);
            map.clear();
        }
        // 2  删除菜品数据
        super.removeByIds(idList);
    }

    /**
     * 修改setmeal表， setmeal_dish表
     *  setmeal_dish表 中之前与其关联的数据需要先清空，再保存新的
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateWithDishes(SetmealDto setmealDto) {
        // 1、先清理并保存setmeal_dish表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        for (SetmealDish dish : list){
            setmealDishService.removeById(dish.getId());
        }
        // 2、保存新的菜品表 -- 需要重新遍历菜品绑定套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map( (item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        // 保存套餐和菜品的关联关系操纵setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据套餐id查询套餐详情，包括与其关联的菜品id
     *
     * @param id
     * @return SetmealDto 套餐扩展类
     */
    @Transactional
    @Override
    public SetmealDto getWithDishes(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        // 查询关联的菜品
        LambdaQueryWrapper<SetmealDish> setmealDishQW = new LambdaQueryWrapper<>();
        setmealDishQW.eq(SetmealDish::getSetmealId, id);
        setmealDto.setSetmealDishes(setmealDishService.list(setmealDishQW));
        return setmealDto;
    }


}